
#include <BEnum>
#include "member_BEnum.h"
#include "member_BSource.h"

using namespace BWE;

#define member					(*(member_BEnum*)_ptr)
#define member_allocate()		_ptr = new member_BEnum(this)
#define member_release()		delete (member_BEnum*)_ptr

BEnum::BEnum(const BString& name) : BObject(name)
{
	member_allocate();
}
BEnum::~BEnum()
{
	member_release();
}

void BEnum::copy(const BEnum* other)
{
	this->setName(other->name());
	member.type = enum_member(other)->type;
	member.pairs = enum_member(other)->pairs;
}
BEnum* BEnum::clone() const
{
	BEnum* enm = new BEnum(this->name());
	enm->copy(this);
	return enm;
}

bool BEnum::empty() const
{
	return member.pairs.empty();
}

bool BEnum::append(const char* key, int value)
{
	if (member.pairs.contain(key))
		return false;
	EnumPair& pair = member.pairs.append();
	pair.key = key;
	pair.value = value;
	return true;
}
bool BEnum::append(const BString& key, int value)
{
	if (member.pairs.contain(key))
		return false;
	if (value < 0)
	{
		if (member.pairs.empty())
			value = 0;
		else
			value = member.pairs.last().value + 1;
	}
	EnumPair& pair = member.pairs.append();
	pair.key = key;
	pair.value = value;
	return true;
}
bool BEnum::contain(const char* key) const
{
	return member.pairs.contain(key);
}
bool BEnum::contain(const BString& key) const
{
	return member.pairs.contain(key);
}
bool BEnum::remove(const char* key)
{
	return member.pairs.remove(key);
}
bool BEnum::remove(const BString& key)
{
	return member.pairs.remove(key);
}
bool BEnum::clear()
{
	return member.pairs.clear();
}

int BEnum::keyCount() const
{
	return member.pairs.size();
}
const BString& BEnum::key(int index) const
{
	return member.pairs(index).key;
}

int BEnum::indexOfValue(int value) const
{
	return member.pairs.find(value);
}
const BString& BEnum::keyOfValue(int value) const
{
	int index = member.pairs.find(value);
	EnumPair& pair = member.pairs(index);
	return pair.key;
}

int BEnum::value(int index) const
{
	return member.pairs(index).value;
}
int BEnum::value(const char* key) const
{
	int index = member.pairs.find(key);
	EnumPair& pair = member.pairs(index);
	return pair.value;
}
int BEnum::value(const BString& key) const
{
	int index = member.pairs.find(key);
	EnumPair& pair = member.pairs(index);
	return pair.value;
}

bool BEnum::input(const BString& text, const BValue& value)
{
	return BObject::input(text, value);
}
bool BEnum::query(const BString& text, BValue& value) const
{
	return BObject::query(text, value);
}
